/**
 * Copyright  2025, Hai Yue Xing He  ZHAO LIMIN
 * 
 * @author        ZHAO LIMIN
 * @version       1.0.1
 * @since         2025-04
 * 坐标转换工具，用于进行地理坐标系（M)转向屏幕坐标系
 */
import {HYXH_GRAVITY, HYXH_RESOLUTION_RATIO} from "../HYXHCommon";      //常量
import {Vec2 } from "cc";  
/**
 * 坐标转换算法
 */
export class HYXHCoordTools {
    /**
     * 世界坐标转屏幕坐标
     * @param worldPos 世界坐标系中的位置
     * @param viewport 视口设置
     * @returns 屏幕坐标
     */
    static worldToScreen(
        worldPos: { x: number; y: number },
        viewport: {
            center: { x: number; y: number };
            zoom: number;
            screenSize: { width: number; height: number };
            yFlip?: boolean; // 是否翻转Y轴（屏幕坐标系通常Y轴向下）
        }
    ): { x: number; y: number } {
        const screenX = (worldPos.x - viewport.center.x) * viewport.zoom + viewport.screenSize.width / 2;
        let screenY = (worldPos.y - viewport.center.y) * viewport.zoom + viewport.screenSize.height / 2;
        
        if (viewport.yFlip) {
            screenY = viewport.screenSize.height - screenY;
        }
        
        return { x: screenX, y: screenY };
    }

    /**
     * 世界坐标转屏幕坐标
     * @param worldPos 世界坐标系中的位置
     * @param viewport 视口设置
     * @returns 屏幕坐标
     */
    static worldToScreen_vec2(
        worldPos        :   Vec2,
        viewport        :   {
            center      : Vec2;
            zoom        : number;
            screenSize  : { width: number; height: number };
            yFlip?      : boolean; // 是否翻转Y轴（屏幕坐标系通常Y轴向下）
        }
    ): Vec2 {
        const screenX = (worldPos.x - viewport.center.x) * viewport.zoom + viewport.screenSize.width / 2;
        let screenY = (worldPos.y - viewport.center.y) * viewport.zoom + viewport.screenSize.height / 2;
        
        if (viewport.yFlip) {
            screenY = viewport.screenSize.height - screenY;
        }        
        return new Vec2(screenX, screenY); 
    }
}